﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


using DotNet.Business;
using DotNet.Model;
using DotNet.Common;
using System.IO;
using System.Collections.Generic;
public partial class Admin_adminPower : basePage
{
    PowerBll pBll = new PowerBll();
    rolePowerBll rpBll = new rolePowerBll();
    int pagesize = 20;
    public string input = "<input id='HiddenUrl' type='hidden' value='&CurrentIndex=1' />";
    private static readonly object objSync = new object();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["state"] != null)
            {
                string state = Request.QueryString["state"].ToString();
                int powerid = Convert.ToInt32(Request.QueryString["pid"]);
                switch (state.Trim())
                {
                    case "insert":
                        InsertPower();
                        break;
                    case "delAll":
                        delAll(powerid);
                        break;
                    case "delTb":
                        delTb(powerid);
                        break;
                    default:
                        break;
                }             
            }
            bindPower();
          
        }
    }
    #region delele
    private void delTb(int powerid)
    {
        power p = pBll.GetModel(powerid);
        IList<rolePower> rpList = rpBll.GetModel();
        lock (objSync)
        {
            foreach (rolePower rp in rpList)
            {
                if (rp.PowerId.Contains("|"))
                {
                    string newPowerId = "";
                    IList<string> strPowerId = rp.PowerId.Split('|');
                    if (!strPowerId.Contains(powerid.ToString()))
                    {
                        continue;
                    }
                    foreach (string id in strPowerId)
                    {
                        if (powerid != Convert.ToInt32(id))
                            newPowerId += id + "|";
                    }
                    if (newPowerId != "")
                    {
                        newPowerId = newPowerId.Remove(newPowerId.Trim().Length - 1, 1);
                        rp.PowerId = newPowerId;
                        rpBll.Update(rp);
                    }
                }
                if (p.PowerId == powerid)
                    rpBll.DeleteByPowerId(powerid.ToString());
            }
        }
        Response.Write("<font color='red'>解除成功</font>");
    }

    private void delAll(int powerid)
    {
        bool bo = false;
        power p = pBll.GetModel(powerid);
        IList<rolePower> rpList = rpBll.GetModel();
        foreach (rolePower rp in rpList)
        {
            if (bo)
            {
                break;
            }
            if (rp.PowerId.Contains("|"))
            {
                string[] strPowerId = rp.PowerId.Split('|');
                foreach (string id in strPowerId)
                {
                    if (p.PowerId == Convert.ToInt32(id))
                    {
                        bo = true;
                        break;
                    }
                }
            }
            else
            {
                if (p.PowerId ==Convert.ToInt32(rp.PowerId))
                    bo = true;
            }
        }
        if (!bo)
        {
            if (pBll.Delete(powerid))
            {

            }
        }
        else
        {
            Response.Write("<font color='red'>权限已经被角色使用</font>");
        }
    }
    #endregion

    #region 绑定权限---郑同飞
    /// <summary>
    /// 绑定权限---郑同飞
    /// </summary>
    public void bindPower() 
    {
        if (Request.QueryString["CurrentIndex"]!=null)
        {
            _currentIndex = Convert.ToInt32(Request.QueryString["CurrentIndex"]);
        }
        DataTable dt = pBll.pageingForPower(_currentIndex, pagesize);
        if (dt.Rows.Count==0&&_currentIndex>1)
        {
            --_currentIndex;
            dt = pBll.pageingForPower(_currentIndex, pagesize);
        }
        input = "<input id='HiddenUrl' type='hidden' value='&CurrentIndex=" + _currentIndex+"' />";

        this.Repeater1.DataSource = dt;
       this.Repeater1.DataBind();
    }
    #endregion

    #region 分页按钮---郑同飞
    /// <summary>
    /// 分页按钮---郑同飞
    /// </summary>
    public void htmlPage() 
    {
        string str = null;
        int count = 0;
        count = pBll.getRecordCount();
        int pcount = count % pagesize == 0 ? count / pagesize : count / pagesize + 1;
        str = "页次：" + _currentIndex + "/" + pcount + "  每页" + pagesize + " 总收录数据" + count + "条";
        str += HtmlControls.pagIng(_currentIndex, pcount, "adminPower.aspx?CurrentIndex", "pageaa", "pagecurrent");
        Response.Write(str);
    }
    #endregion

    #region 添加权限
    /// <summary>
    /// 添加权限
   /// </summary>
    private void InsertPower()
    {
        
        power p = new power();
        p.Demoinfo = Request.Form["demoinfo"].ToString().Trim();
        p.DemoName = Request.Form["demoName"].ToString().Trim();
        p.Powername = Request.Form["Powername"].ToString().Trim();
        p.Powerpage = Request.Form["page"] ?? "";
        if (p.Demoinfo.Trim() == "" || p.DemoName.Trim() == "" || p.Powername.Trim() == "" || p.Powerpage.Trim() == "" || pBll.Exists(p.Powerpage,p.Powername))
        {
            Response.Write("必填信息需完整! 或则数据重复");
            Response.End();
        }
        if (pBll.Insert(p))
        {
            Response.Write("1");
            Response.End();
        }

    }
 #endregion

    #region 获得后台管理的页面，本页面有程序员操作
    /// <summary>
    /// 获得后台管理的页面，本页面有程序员操作
    /// </summary>
    /// <returns></returns>
    public string pageHtml()
    {
       
        string pageStr = "";
        string[] pages = Directory.GetFiles(Server.MapPath(@"~/Admin/"));
        foreach (string page in pages)
        {
            if (page.EndsWith(".aspx"))
            {
                if(!pBll.Exists(Path.GetFileName(page)))
                pageStr += "<input id=\"Radio1\" type=\"radio\" value=" + Path.GetFileName(page) + " name=\"page\">" + Path.GetFileName(page) + "</input>"+"<br>";
            }
        }
        return pageStr;
       
    } 
    #endregion

    #region 状态维护---郑同飞
    private int _currentIndex = 1;
    #endregion
}
